package miaofusdk

import (
	"encoding/json"
	"errors"
	"fmt"
)

type UserBean struct {
	Id            int    `json:"id,omitempty,string"`
	UserName      string `json:"user_name,omitempty"`
	Password      string `json:"password,omitempty"`
	NickName      string `json:"nick_name,omitempty"`
	Sign          string `json:"sign,omitempty"`
	Postal        string `json:"postal,omitempty"`
	Address       string `json:"address,omitempty"`
	HeadImg       string `json:"headimg,omitempty"`
	Cover         string `json:"cover,omitempty"`
	Sex           int    `json:"sex,omitempty,string"`
	Age           int    `json:"age,omitempty,string"`
	Birthday      string `json:"birthday,omitempty"`
	Constellation string `json:"constellation,omitempty"`
	Company       string `json:"company,omitempty"`
	School        string `json:"school,omitempty"`
	Type          int    `json:"type,omitempty,string"`
	PushId        string `json:"push_id,omitempty"`
}

type RespUser struct {
	Response
	Data UserBean `json:"data,omitempty"`
}

type RespUsers struct {
	Response
	Page       int        `json:"page,string"`
	Rows       int        `json:"rows,string"`
	TotalCount int        `json:"total_count,string"`
	TotalPage  int        `json:"total_page,string"`
	List       []UserBean `json:"list,omitempty,string"`
}

func (s *SrvUserManager) GetUserById(id int) (UserBean, error) {
	resp := RespUser{}
	http_request := s.GetPost(s.GetUrl() + "/user/read")
	http_request.Param("uid", fmt.Sprintf("%d", id))
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(fmt.Sprintf("%s:%s", resp.Code, resp.Msg))
	} else {
		return resp.Data, nil
	}
}

func (s *SrvUserManager) Login(user_name, password string) (UserBean, error) {
	resp := RespUser{}
	http_request := s.GetPost(s.GetUrl() + "/user/login")
	http_request.Param("user_name", user_name)
	http_request.Param("password", password)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(fmt.Sprintf("%s:%s", resp.Code, resp.Msg))
	} else {
		return resp.Data, nil
	}
}
